package it.traveldreamWeb.customer;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import it.traveldream.dto.TransportMeanDTO;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

@FacesConverter(value="dateConverter")
public class DateConverter implements Converter {

	public final static DateFormat df = new SimpleDateFormat("MMddyyyy");
	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
		if (arg2 == null || arg2.trim().equals(""))   
            return null;  
          
        try{
        	return df.parse(arg2);
        } catch(Exception exception) {  
            throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid mean"));  
        } 
	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
		String str = "";  
        if (arg2 instanceof TransportMeanDTO)
        	str = "" + ((TransportMeanDTO)arg2).getDeparture().getName();
        return str;
	}

}
